/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package VMCategoriesSettings;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

import javax.swing.*;
import GuiTools.*;
import Mainframe.Mainframe;
import Settings.*;
import Storage.*;
import VocabularyManagement.EditVocDialog;

/**
 * @author Christoph Schnack
 *
 */
public class VMCategoriesSettings {
	Settings SET;
	Language LANG;
	Storage MAINVOC;
	Mainframe MAINFRAME;
	Vocabulary vocToEdit;
	MDManager VMDM;
	
	MDTribuitFrame frame;
	DefaultListModel dlmA = new DefaultListModel(),dlmB = new DefaultListModel();
	CategoryListPanel lpA,lpB;
	
	public VMCategoriesSettings(String vte,Storage stor,Settings set,Mainframe mf,MDManager vmdm){
		SET = set;
		LANG = set.getLanguage();
		vocToEdit = stor.getVocabulary(vte);
		MAINVOC = stor;
		VMDM = vmdm;
		MAINFRAME = mf;
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				buildGUI();
			}
		});
	}
	
	public void buildGUI(){
		frame = new MDTribuitFrame(LANG.getEditCategoriesTitle(),VMDM);
		
		for(String s : vocToEdit.getCategoriesA()){
			if(!s.equals("n.a."))dlmA.addElement(s);
		}
		for(String s : vocToEdit.getCategoriesB()){
			if(!s.equals("n.a."))dlmB.addElement(s);
		}
		lpA = new CategoryListPanel(frame, LANG, dlmA,vocToEdit.getNameA());
		lpB = new CategoryListPanel(frame, LANG, dlmB,vocToEdit.getNameB());
		
	//center panel
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		centerPanel.setBackground(Color.darkGray);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(20,20,20,10);
		centerPanel.add(lpA,gbc);
		gbc.insets = new Insets(20,0,20,20);
		centerPanel.add(lpB,gbc);
		
	//buttonPanel		
		JButton applyBtn = new JButton(LANG.getApply());
		applyBtn.addActionListener(new ApplyBtnListener());
		JButton cancelBtn = new JButton(LANG.getDiscardAndCancel());
		cancelBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				frame.dispose();
				new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
			}
		});
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(applyBtn);
		buttonPanel.add(cancelBtn);
		buttonPanel.setBackground(GuiTools.BGCOLOR);
		
		frame.getContentPane().add(BorderLayout.NORTH,new Header(LANG.getEditCategoriesTitle()));
		frame.getContentPane().add(BorderLayout.CENTER,centerPanel);
		frame.getContentPane().add(BorderLayout.SOUTH,buttonPanel);
		frame.showUpAt(MAINFRAME);
	}
	
	private class ApplyBtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			Vector<String> newA = new Vector<String>();
			newA.addElement("n.a.");
			if(dlmA.size()>0){
				for(int i = 0;i<dlmA.size();i++){
					newA.addElement((String)dlmA.elementAt(i));
				}
			}
			newA.trimToSize();
			Vector<String> newB = new Vector<String>();
			newB.addElement("n.a.");
			if(dlmB.size()>0){
				for(int i = 0;i<dlmB.size();i++){
					newB.addElement((String)dlmB.elementAt(i));
				}
			}
			newB.trimToSize();
			boolean differenceA = false,differenceB = false;
			if(vocToEdit.getCategoriesA().size() == newA.size()){
				for(String s : vocToEdit.getCategoriesA()){
					if(!newA.contains(s)){
						differenceA = true;
						break;
					}
				}
				if(!differenceA){
					vocToEdit.setCategoriesA(newA);
				}	
			}
			else differenceA = true;
			if(vocToEdit.getCategoriesB().size() == newB.size()){
				for(String s : vocToEdit.getCategoriesB()){
					if(!newB.contains(s)){
						differenceB = true;
						break;
					}
				}
				if(!differenceB){
					vocToEdit.setCategoriesB(newB);
				}	
			}
			else differenceB = true;
			if(!differenceA && !differenceB){
				frame.dispose();
				MAINVOC.saveVocabularyIfAutosave(vocToEdit);
				new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
			}
			else{
				frame.setVisible(false);
				new ApplyChangesDialog(MAINFRAME,MAINVOC,SET,VMDM,frame,vocToEdit,newA,differenceA,newB,differenceB);
			}
		}
	}
}
